package com.tb.controller;

import com.tb.dubbo.IOrderItemService;
import com.tb.dubbo.IcommentsService;
import com.tb.pojo.Comments;
import com.tb.pojo.User;
import com.tb.utils.Msg;
import com.tb.utils.Utf8;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentsController {
    @Autowired
    private IcommentsService commentsService;

    @Autowired
    private IOrderItemService orderItemService;
    /*商品评论*/
    @GetMapping("/list")
    public Msg getComments(@RequestParam(name="goods_id",defaultValue = "1") String goods_id){

        Map<String,Object> map=new HashMap<>();
        map.put("goods_id",goods_id);
        List<Comments> comments=commentsService.getAllComments(map);
        return Msg.success(comments);
    }

    /**
     * @api {get} /comments/addComments 新增评论 万旭刚
     * @apiDescription 新增商品评论 &nbsp;&nbsp;<span style="color:#D0254E">[*需要在用户登陆状态下请求]</span>
     * @apiGroup Comments API
     *
     * @apiParam {String} comments 评论详情 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam {String} goodsId 商品编号 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam {int} anonStatus 是否匿名(1，匿名 0 不匿名)
     * @apiParam {int} staifaction 满意度(1-5)
     * @apiParam {int} orderItemId 商品订单详情编号
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     *          "comments":"千峰的服务真实太好了",
     *          "goodsId":"1",
     *          "anonStatus":"0",
     *          "staifaction":"5",
     *          "orderItemId":1
     *      }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccessExample {json} 正确返回值:
     *      {
     *     "code": 0,
     *     "msg": "",
     *     "data":null
     *    }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 1,
     *     "msg": "参数不合法(请求登陆超时)",
     *     "data": null
     * }
     *
     * @apiSampleRequest /comments/addComments
     */
    @GetMapping("/addComments")
    public Msg addComments(@RequestParam(name = "orderItemId")int orderItemId,
                           @RequestParam(name="comments")String comment,
                           @RequestParam(name="goodsId")String goodsId,
                           @RequestParam(name="anonStatus",defaultValue = "0")int anonStatus,
                           @RequestParam(name="staifaction")int staifaction){
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        try{
            User user = (User) session.getAttribute("User");
            String userId = user.getUserId();
            Comments comments = new Comments();
            comments.setUserId(userId);
            comments.setComments(Utf8.getString(comment));
            comments.setAnonStatus(anonStatus);
            comments.setStaifaction(staifaction);
            comments.setGoodsId(goodsId);
            int i = commentsService.insert(comments);
            orderItemService.updateCommentsStatus(orderItemId);
            if(i<1){
                return Msg.error(1,"参数不合法");
            }else{
                return Msg.success(null);
            }
        }catch (Exception e){
            return Msg.error(999,"请求登陆超时");
        }
    }
}
